<template>
  <div class="y-conter">
    <div class="y-biaoge">
      <h3 class="y-bold">消息列表</h3>
      <!-- 搜索输入框部分 -->
      <div>
        <el-form
          :inline="true"
          ref="ruleForm"
          :model="ruleForm"
          class="demo-form-inline"
        >
          <!-- 按钮搜和索部分 -->
          <el-form-item label="全部类型:">
             <el-select v-model="ruleForm.message_type" placeholder="请选择">
                <el-option label="系统通知" :value="0"></el-option>
                <el-option label="任务通知" :value="1"></el-option>
                <el-option label="消息通知" :value="2"></el-option>
              </el-select>
            <!-- 按钮搜和索部分 -->
          </el-form-item>
          <!-- 按钮搜和索部分 -->
          <el-form-item>
            <el-button type="primary" @click="search('ruleForm')"
              >搜索</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button type="primary" @click="Newbtn()"> 新增 </el-button>
          </el-form-item>
        </el-form>
        <!-- 对话框 -->
        <el-dialog title="新增信息" :visible.sync="dialogVisible" width="700px">
          <el-form
            :model="formInline"
            status-icon
            :rules="rules"
            ref="formInline"
          >
            <el-row>
              <el-col :span="6">
                <el-form-item prop="message_type">
                  <div>消息类型:</div>
                  <el-select
                    v-model="formInline.message_type"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in input2"
                      :key="item.index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="标题:">
                  <el-input
                    v-model="formInline.message_head"
                    placeholder="请输入"
                     class="padding"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="发布人"  prop="message_send">
                    <el-select v-model="formInline.message_send" placeholder="请选择">
                      <el-option label="刘杰" :value="0"></el-option>
                      <el-option label="李三" :value="1"></el-option>
                       <el-option label="刘培" :value="2"></el-option>
                    </el-select>
                  </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="详细信息" prop="message_txt">
              <el-input class="y-textarea"
                type="textarea" 
                v-model="formInline.message_txt"
              
              ></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="quxiao('formInline')">取 消</el-button>
            <el-button type="primary" @click="SureEdit('formInline')"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </div>
      <!-- 表格部分 -->
      <div class="y-table">
        <el-table
          :border="true"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          ref="multipleTable"
          :data="currList"
          tooltip-effect="dark"
          style="width: 98%, text-align: center "
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            align="center"
            type="index"
            width="100"
            :index="indexFn"
            label="序号"
          ></el-table-column>
          <el-table-column
            label="消息类型"
            prop="message_type"
            width="200"
            hight="30px"
            align="center"
          >
           <template slot-scope="scope">
              <span v-if="scope.row.message_type=='0'">系统通知</span>
              <span v-if="scope.row.message_type=='1'">消息通知</span>
              <span v-if="scope.row.message_type=='2'">任务通知</span>
            </template>
          </el-table-column>
          <el-table-column
            label="标题"
            prop="message_head"
            width="280"
            align="center"
          ></el-table-column>
          <el-table-column
            label="发布人"
            prop="message_send"
            width="180"
            align="center"
          >
           <template slot-scope="scope">
              <span v-if="scope.row.message_send=='0'">刘杰</span>
              <span v-if="scope.row.message_send=='1'">李三</span>
              <span v-if="scope.row.message_send=='2'">刘培</span>
            </template>
          </el-table-column>
          <el-table-column
            label="时间"
            width="280"
            prop="message_date"
            align="center"
          ></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleEdit(scope.$index, scope.row)"
                >详情</el-button
              >
              <el-button
                type="text"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 对话框 -->

        <el-dialog
          title="详细信息"
          :visible.sync="dialogTableVisible"
          width="30%"
        >
          <p>
            <span class="y-1">消息类型:</span>
            <span class="y-2"  v-if="this.shuju.message_type=='0'">
              系统通知
            </span>
            <span class="y-2"  v-if="this.shuju.message_type=='1'">
              消息通知
            </span>
            <span class="y-2"  v-if="this.shuju.message_type=='2'">
              任务通知
            </span>
          </p>
            <p>
              <span class="y-1">标题:</span>
              <span class="y-2">&nbsp;{{ this.shuju.message_head }}</span>
            </p>
            <p>
              <span class="y-1">发布人:</span>
              <span class="y-2"  v-if="this.shuju.message_send=='0'">
                 刘杰
              </span>
              <span class="y-2"  v-if="this.shuju.message_send=='1'">
                  李三
              </span>
              <span class="y-2"  v-if="this.shuju.message_send=='2'">
                  刘培
              </span>
            </p>
            <p>
              <span class="y-1">时间:</span>
              <span class="y-2">&nbsp;{{ this.shuju.message_date }}</span>
            </p>
            <p>
              <span class="y-1">详细信息:</span>
              <span class="y-2">&nbsp;{{ this.shuju.message_txt }}</span>
            </p>
        </el-dialog>
      </div>
      <!-- 分页器 -->
      <div class="y-fenye">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="tableData.length"
          :page-size="pageSize"
          @current-change="changePage"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import http from "../api";
export default {
  data() {
    return {
      //消息类型
      input2: [
        {
          value: 0,
          label: "任务通知",
        },
        {
          value: 1,
          label: "消息通知",
        },
        {
          value: 2,
          label: "系统通知",
        },
      ],
      input1: "", //单位
      //搜索输入框
      ruleForm: {
        message_type: "", //消息类型
      },
      dialogVisible: false, //新增
      dialogTableVisible: false, //详情

      // 新增弹框
      formInline: {
        message_type: "", //消息类型
        message_head: "", //标题
        message_send: "", //发布人
        message_txt: "", //详细信息
      },
      rules: {}, //验证
      // 详细信息
      shuju: {
        message_type: "", //消息类型
        message_head: "", //标题
        message_send: "", //发布人
        message_txt: "", //详细信息
      },
      //多选表格的复选框
      multipleSelection: [],
      // 分页
      tableData: [], //总数居
      currPage: 1, //当前页
      pageSize: 5, //每页数据量
      currList: [], //当前展示的数据数组
      pickerOptions1: {
        //  日期
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      value1: "",
      value2: "",
    };
  },
  computed: {},

  methods: {
    // 分页
    setCurrList(index) {
      this.currList = this.tableData.slice(
        (index - 1) * this.pageSize,
        index * this.pageSize
      );
      // console.log('当前页',index);
    },
    //改变当前页
    changePage(val) {
      this.currPage = val;
      this.setCurrList(val);

      // console.log(val);
    },
    // 表格序号
    indexFn(index) {
      index = index + 1 + (this.currPage - 1) * this.pageSize;
      return index;
    },
    //重置按钮
    resetForm(formName) {
      this[formName] = {};
      this.$refs[formName].resetFields();
    },

    // 新增按钮
    Newbtn(index, row) {
      this.dialogVisible = true;
      console.log("111");
    },
    // 新增里的确定按钮
    SureEdit(formName) {
      console.log(this.formInline);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogVisible = false;
          http.addmessage(this.formInline).then((r) => {
            this.shuju.message_txt =this.formInline.message_txt; //内容
            this.formInline={};

            console.log(r);
            http.inquire({}).then((r) => {
             
              console.log(r);
              this.tableData = r.data;
              console.log(r.data);
              this.setCurrList(1);
            });
            
          });
        }
      });
    },

    // 新增里的取消按钮
    quxiao(formInline) {
      console.log(this.formInline);
      this.dialogVisible = false;
     
    },

    // 表格删除按钮
    handleDelete(index, row) {
      http.remove(row).then((r) => {
        http.inquire(this.formInline).then((r) => {
          this.tableData = r.data;
          console.log("bbbbb", r);
          this.setCurrList(1);
        });
        console.log(r);
        console.log("单个删除", r.data);
      });
    },
    //详情
    handleEdit(index, row) {
      console.log(row);
      this.dialogTableVisible = true;
      this.shuju.message_type = row.message_type; //消息
      this.shuju.message_head = row.message_head; //标题
      this.shuju.message_send = row.message_send; //发布人
      this.shuju.message_date = row.message_date; //时间
      this.shuju.message_txt = row.message_txt; //内容

      console.log(this.shuju);
     
    },
    //选中复选框获取下标
    handleSelectionChange(val) {
      // this.multipleSelection = val;
      // let ids = [];
      // this.multipleSelection.map((item) => {
      //   ids.push(item.key);
      // });
      // this.selectedIDs = ids;
      // console.log("你点击的是", this.selectedIDs);
    },

    // //搜索输入
    search(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          http.inquire(this.ruleForm).then((r) => {
            console.log("返回的数据", r);
            this.tableData = r.data;
            this.setCurrList(1);
            console.log(this.ruleForm);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  //获取数据
  mounted() {
    http.inquire({}).then((r) => {
      console.log(r);
      this.tableData = r.data;
      console.log(r.data);
      this.setCurrList(1);
    });
  },
};
</script>
<style scoped>
.padding >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px !important;
}
.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px;
  /* margin-left: 5px; */
}
/* 大容器 */
.y-conter {
  height: 640px;
  background-color: white;
  overflow: hidden;
  overflow-y: scroll;
  margin-top: 15px;
}
/* 表格 */
.y-biaoge {
  margin-top: 20px;
  margin-left: 10px;
}

.y-table {
  margin-top: 15px;
}
/* 右侧新增导出按钮 */
.y-btn {
  position: absolute;
  top: 164px;
  left: 94%;
}
/* 所有按钮 */
.el-button {
  height: 30px;
  line-height: 0px;
}
/* 标题字体 */
.y-bold {
  font-weight: bold;
}
/* 输入框部分样式 */
.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
}
.el-input >>> .el-input__inner {
  width: 178px;
  height: 30px;
}
.el-range-editor {
  width: 160px;
  height: 30px;
  padding-left: 10px;
}
.el-input >>> .el-range__icon {
  line-height: 0px;
  padding-left: 10px;
}
.el-range-editor >>> .el-input__inner {
  width: 200px;
  height: 30px;
  margin-top: 10px;
}
/* 分页 */
.y-fenye {
  float: right;
  margin-top: 20px;
}
/* 新增 */

.el-form-item {
  margin-bottom: 2px !important;
}
/* 输入框 */
.el-col-6[data-v-48b1ad5f] {
  width: 20% !important;
  margin-left: 60px;
}
/* 文本域 */
.el-textarea__inner {
  width: 89%;
}
/* 文本域 */
.y-textarea{
  width: 90%;
  margin-left: 55px;
}
</style>